package com.spark.tags;

import java.util.Map;

import com.spark.core.SparkContext;
import com.spark.core.XMap;

public class TreeGrid extends Grid {	
	
	private static final String DEFAULT_TEMPLATES = "treeGrid.ftl";
	
	
	public static final String PROPERTITY_RENDERER = "renderer";
	public static final String PROPERTITY_FIELD_ID = "idField";
	public static final String PROPERTITY_SHOWCHECKBOX = "showCheckBox";
	public static final String PROPERTITY_FIELD_PARENT = "parentField";
	public static final String PROPERTITY_FIELD_DISPLAY = "displayField";
	public static final String PROPERTITY_FIELD_EXPAND = "expandField";
	public static final String PROPERTITY_FIELD_CHECKED = "checkField";
	public static final String PROPERTITY_FIELD_SEQUENCE = "sequenceField";
	public static final String PROPERTITY_FIELD_ICON = "iconField";
	public static final String PROPERTITY_EXPAND_ALL = "expandAll";
	
	private static final String DEFAULT_CLASSNAME = "s-treegrid";
	private static final String DEFAULT_ID_FIELD = "id";
	private static final String DEFAULT_PARENT_FIELD = "pid";
	private static final String DEFAULT_DISPLAY_FIELD = "name";
	private static final String DEFAULT_EXPAND_FIELD ="expanded";
	private static final String DEFAULT_SEQUENCE_FIELD = "sequence";
	private static final String DEFAULT_CHECKED_FIELD = "checked";
	private static final String DEFAULT_ICON_FIELD = "icon";
	
	protected String getDefaultClass(XMap view,SparkContext context){
		String className = super.getDefaultClass(view, context);
		StringBuilder sb = new StringBuilder();
		sb.append(className);
		if(!"".equals(className.trim()))sb.append(" ");
		sb.append(DEFAULT_CLASSNAME);
		return sb.toString();
	}
	
	
	public String getTemplate(){
		return DEFAULT_TEMPLATES;
	}
	
	
	public void init(XMap view, SparkContext context) throws Exception{
		super.init(view, context);
		
		if(context.getContextPath()!=null) context.addJsonConfig(Tree.CONFIG_CONTEXT,context.getContextPath()+"/");
		context.addJsonConfig(PROPERTITY_FIELD_DISPLAY, view.getString(PROPERTITY_FIELD_DISPLAY,DEFAULT_DISPLAY_FIELD));
		String renderer = view.getString(PROPERTITY_RENDERER);
		if(renderer!=null)context.addJsonConfig(PROPERTITY_RENDERER, renderer);
		context.addJsonConfig(PROPERTITY_FIELD_ID, view.getString(PROPERTITY_FIELD_ID,DEFAULT_ID_FIELD));
		context.addJsonConfig(PROPERTITY_FIELD_PARENT, view.getString(PROPERTITY_FIELD_PARENT,DEFAULT_PARENT_FIELD));
		context.addJsonConfig(PROPERTITY_EXPAND_ALL,  view.getBoolean(PROPERTITY_EXPAND_ALL,false));
		context.addJsonConfig(PROPERTITY_SHOWCHECKBOX,  view.getBoolean(PROPERTITY_SHOWCHECKBOX,false));
		context.addJsonConfig(PROPERTITY_FIELD_CHECKED, view.getString(PROPERTITY_FIELD_CHECKED,DEFAULT_CHECKED_FIELD));
		context.addJsonConfig(PROPERTITY_FIELD_EXPAND, view.getString(PROPERTITY_FIELD_EXPAND,DEFAULT_EXPAND_FIELD));
		context.addJsonConfig(PROPERTITY_FIELD_ICON, view.getString(PROPERTITY_FIELD_ICON, DEFAULT_ICON_FIELD));
		context.addJsonConfig(PROPERTITY_FIELD_SEQUENCE, view.getString(PROPERTITY_FIELD_SEQUENCE,DEFAULT_SEQUENCE_FIELD));
		context.addJsonConfig(PROPERTITY_FOCUS_ROW, false);
	}
	
	
	@SuppressWarnings("unchecked")
	protected String build(SparkContext context){
		Map map = context.getMap();
		StringBuilder sb = new StringBuilder();
		sb.append("<TABLE class=\"").append(get(map,"className")).append("\" id=\"").append(get(map,"id")).append("_wrap\" style=\"").append(get(map,"style")).append("\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\r");
		sb.append("<TBODY>\r");
		sb.append("    ").append(get(map,"toolBar")).append("  \r");
		sb.append("    <TR>\r");
		sb.append("        <TD class=\"layout-td-cell\">\r");
		sb.append("            <DIV class=\"s-grid\" id=\"").append(get(map,"id")).append("\" style=\"width:").append(get(map,"width")).append("px;height:").append(get(map,"tableHeight")).append("px;").append(get(map,"gridStyle")).append("\">\r");
		sb.append("                <A stype=\"grid.focus\" href=\"#\" class=\"grid-fs\" tabIndex=\"-1\"></A>\r");
		sb.append("                ").append(get(map,"lockArea"));
		sb.append("                <DIV class=\"grid-ua\" stype=\"grid.uc\" style=\"width:").append(get(map,"unlockWidth")).append("px;\">\r");
		sb.append("                    <DIV class=\"grid-uh\" unselectable=\"on\" onselectstart=\"return false;\" style=\"width:").append(get(map,"unlockWidth")).append("px;-moz-user-select:none;text-align:left;height:").append(get(map,"headHeight")).append("px;\"  stype=\"grid.uh\">\r");
		sb.append("                        ").append(get(map,"unlockArea"));
		sb.append("                    </DIV>\r");
		sb.append("                    <DIV class=\"grid-ub\" stype=\"grid.ub\" style=\"width:").append(get(map,"unlockWidth")).append("px;height:").append(get(map,"bodyHeight")).append("px;text-align:left;\">\r");
		sb.append("                    </DIV>\r");
		sb.append("                </DIV>\r");
		sb.append("                <DIV style=\"clear:both;\"></DIV>\r");
		sb.append("                <DIV stype=\"grid.spliter\" class=\"grid-sp\" onselectstart=\"return false;\" unselectable=\"on\"  style=\"display:none;\"></DIV>\r");
		sb.append("            </DIV>\r");
		sb.append("        </TD>\r");
		sb.append("    </TR>\r");
		sb.append("    ").append(get(map,"footerBar"));
		sb.append("    ").append(get(map,"navBar"));
		sb.append("</TBODY>\r");
		sb.append("</TABLE>\r");
		sb.append("<SCRIPT LANGUAGE=\"JavaScript\">new Spark.TreeGrid(").append(get(map,"config")).append(");</SCRIPT>\r");
		sb.append(get(map,"editors"));
		return sb.toString();
	}
}
